package com.ugoodtech.mdcc.core.model.mapper;

import static org.apache.ibatis.jdbc.SqlBuilder.BEGIN;
import static org.apache.ibatis.jdbc.SqlBuilder.DELETE_FROM;
import static org.apache.ibatis.jdbc.SqlBuilder.FROM;
import static org.apache.ibatis.jdbc.SqlBuilder.INSERT_INTO;
import static org.apache.ibatis.jdbc.SqlBuilder.ORDER_BY;
import static org.apache.ibatis.jdbc.SqlBuilder.SELECT;
import static org.apache.ibatis.jdbc.SqlBuilder.SELECT_DISTINCT;
import static org.apache.ibatis.jdbc.SqlBuilder.SET;
import static org.apache.ibatis.jdbc.SqlBuilder.SQL;
import static org.apache.ibatis.jdbc.SqlBuilder.UPDATE;
import static org.apache.ibatis.jdbc.SqlBuilder.VALUES;
import static org.apache.ibatis.jdbc.SqlBuilder.WHERE;

import com.ugoodtech.mdcc.core.model.entity.StationReport;
import com.ugoodtech.mdcc.core.model.entity.StationReportExample.Criteria;
import com.ugoodtech.mdcc.core.model.entity.StationReportExample.Criterion;
import com.ugoodtech.mdcc.core.model.entity.StationReportExample;
import java.util.List;
import java.util.Map;

public class StationReportSqlProvider {

    public String countByExample(StationReportExample example) {
        BEGIN();
        SELECT("count(*)");
        FROM("station_report");
        applyWhere(example, false);
        return SQL();
    }

    public String deleteByExample(StationReportExample example) {
        BEGIN();
        DELETE_FROM("station_report");
        applyWhere(example, false);
        return SQL();
    }

    public String insertSelective(StationReport record) {
        BEGIN();
        INSERT_INTO("station_report");
        
        if (record.getId() != null) {
            VALUES("id", "#{id,jdbcType=BIGINT}");
        }
        
        if (record.getCreationTime() != null) {
            VALUES("creation_time", "#{creationTime,jdbcType=TIMESTAMP}");
        }
        
        if (record.getDeleted() != null) {
            VALUES("deleted", "#{deleted,jdbcType=BIT}");
        }
        
        if (record.getEnabled() != null) {
            VALUES("enabled", "#{enabled,jdbcType=BIT}");
        }
        
        if (record.getUpdateTime() != null) {
            VALUES("update_time", "#{updateTime,jdbcType=TIMESTAMP}");
        }
        
        if (record.getAdminReaded() != null) {
            VALUES("admin_readed", "#{adminReaded,jdbcType=BIT}");
        }
        
        if (record.getAuditNote() != null) {
            VALUES("audit_note", "#{auditNote,jdbcType=VARCHAR}");
        }
        
        if (record.getAuditing() != null) {
            VALUES("auditing", "#{auditing,jdbcType=VARCHAR}");
        }
        
        if (record.getCategory() != null) {
            VALUES("category", "#{category,jdbcType=VARCHAR}");
        }
        
        if (record.getContactUser() != null) {
            VALUES("contact_user", "#{contactUser,jdbcType=VARCHAR}");
        }
        
        if (record.getDeparmentids() != null) {
            VALUES("deparmentIds", "#{deparmentids,jdbcType=VARCHAR}");
        }
        
        if (record.getEndServiceTime() != null) {
            VALUES("end_service_time", "#{endServiceTime,jdbcType=TIMESTAMP}");
        }
        
        if (record.getExceptionType() != null) {
            VALUES("exception_type", "#{exceptionType,jdbcType=VARCHAR}");
        }
        
        if (record.getImprovementTime() != null) {
            VALUES("improvement_time", "#{improvementTime,jdbcType=BIGINT}");
        }
        
        if (record.getIsFollowed() != null) {
            VALUES("is_followed", "#{isFollowed,jdbcType=BIT}");
        }
        
        if (record.getReAudit() != null) {
            VALUES("re_audit", "#{reAudit,jdbcType=BIT}");
        }
        
        if (record.getReportCategory() != null) {
            VALUES("report_category", "#{reportCategory,jdbcType=VARCHAR}");
        }
        
        if (record.getReportType() != null) {
            VALUES("report_type", "#{reportType,jdbcType=VARCHAR}");
        }
        
        if (record.getScore() != null) {
            VALUES("score", "#{score,jdbcType=INTEGER}");
        }
        
        if (record.getServiceTypeId() != null) {
            VALUES("service_type_id", "#{serviceTypeId,jdbcType=VARCHAR}");
        }
        
        if (record.getSpecifiedType() != null) {
            VALUES("specified_type", "#{specifiedType,jdbcType=VARCHAR}");
        }
        
        if (record.getStartServiceTime() != null) {
            VALUES("start_service_time", "#{startServiceTime,jdbcType=TIMESTAMP}");
        }
        
        if (record.getTemporaryStationId() != null) {
            VALUES("temporary_station_id", "#{temporaryStationId,jdbcType=BIGINT}");
        }
        
        if (record.getUserIds() != null) {
            VALUES("user_ids", "#{userIds,jdbcType=VARCHAR}");
        }
        
        if (record.getStationId() != null) {
            VALUES("station_id", "#{stationId,jdbcType=BIGINT}");
        }
        
        if (record.getUserId() != null) {
            VALUES("user_id", "#{userId,jdbcType=BIGINT}");
        }
        
        if (record.getAnswerOther() != null) {
            VALUES("answer_other", "#{answerOther,jdbcType=VARCHAR}");
        }
        
        if (record.getIsSupplement() != null) {
            VALUES("is_supplement", "#{isSupplement,jdbcType=BIT}");
        }
        
        if (record.getType() != null) {
            VALUES("type", "#{type,jdbcType=VARCHAR}");
        }
        
        if (record.getEditUserId() != null) {
            VALUES("edit_user_id", "#{editUserId,jdbcType=BIGINT}");
        }
        
        if (record.getUsertype() != null) {
            VALUES("userType", "#{usertype,jdbcType=INTEGER}");
        }
        
        return SQL();
    }

    public String selectByExample(StationReportExample example) {
        BEGIN();
        if (example != null && example.isDistinct()) {
            SELECT_DISTINCT("id");
        } else {
            SELECT("id");
        }
        SELECT("creation_time");
        SELECT("deleted");
        SELECT("enabled");
        SELECT("update_time");
        SELECT("admin_readed");
        SELECT("audit_note");
        SELECT("auditing");
        SELECT("category");
        SELECT("contact_user");
        SELECT("deparmentIds");
        SELECT("end_service_time");
        SELECT("exception_type");
        SELECT("improvement_time");
        SELECT("is_followed");
        SELECT("re_audit");
        SELECT("report_category");
        SELECT("report_type");
        SELECT("score");
        SELECT("service_type_id");
        SELECT("specified_type");
        SELECT("start_service_time");
        SELECT("temporary_station_id");
        SELECT("user_ids");
        SELECT("station_id");
        SELECT("user_id");
        SELECT("answer_other");
        SELECT("is_supplement");
        SELECT("type");
        SELECT("edit_user_id");
        SELECT("userType");
        FROM("station_report");
        applyWhere(example, false);
        
        if (example != null && example.getOrderByClause() != null) {
            ORDER_BY(example.getOrderByClause());
        }
        
        return SQL();
    }

    public String updateByExampleSelective(Map<String, Object> parameter) {
        StationReport record = (StationReport) parameter.get("record");
        StationReportExample example = (StationReportExample) parameter.get("example");
        
        BEGIN();
        UPDATE("station_report");
        
        if (record.getId() != null) {
            SET("id = #{record.id,jdbcType=BIGINT}");
        }
        
        if (record.getCreationTime() != null) {
            SET("creation_time = #{record.creationTime,jdbcType=TIMESTAMP}");
        }
        
        if (record.getDeleted() != null) {
            SET("deleted = #{record.deleted,jdbcType=BIT}");
        }
        
        if (record.getEnabled() != null) {
            SET("enabled = #{record.enabled,jdbcType=BIT}");
        }
        
        if (record.getUpdateTime() != null) {
            SET("update_time = #{record.updateTime,jdbcType=TIMESTAMP}");
        }
        
        if (record.getAdminReaded() != null) {
            SET("admin_readed = #{record.adminReaded,jdbcType=BIT}");
        }
        
        if (record.getAuditNote() != null) {
            SET("audit_note = #{record.auditNote,jdbcType=VARCHAR}");
        }
        
        if (record.getAuditing() != null) {
            SET("auditing = #{record.auditing,jdbcType=VARCHAR}");
        }
        
        if (record.getCategory() != null) {
            SET("category = #{record.category,jdbcType=VARCHAR}");
        }
        
        if (record.getContactUser() != null) {
            SET("contact_user = #{record.contactUser,jdbcType=VARCHAR}");
        }
        
        if (record.getDeparmentids() != null) {
            SET("deparmentIds = #{record.deparmentids,jdbcType=VARCHAR}");
        }
        
        if (record.getEndServiceTime() != null) {
            SET("end_service_time = #{record.endServiceTime,jdbcType=TIMESTAMP}");
        }
        
        if (record.getExceptionType() != null) {
            SET("exception_type = #{record.exceptionType,jdbcType=VARCHAR}");
        }
        
        if (record.getImprovementTime() != null) {
            SET("improvement_time = #{record.improvementTime,jdbcType=BIGINT}");
        }
        
        if (record.getIsFollowed() != null) {
            SET("is_followed = #{record.isFollowed,jdbcType=BIT}");
        }
        
        if (record.getReAudit() != null) {
            SET("re_audit = #{record.reAudit,jdbcType=BIT}");
        }
        
        if (record.getReportCategory() != null) {
            SET("report_category = #{record.reportCategory,jdbcType=VARCHAR}");
        }
        
        if (record.getReportType() != null) {
            SET("report_type = #{record.reportType,jdbcType=VARCHAR}");
        }
        
        if (record.getScore() != null) {
            SET("score = #{record.score,jdbcType=INTEGER}");
        }
        
        if (record.getServiceTypeId() != null) {
            SET("service_type_id = #{record.serviceTypeId,jdbcType=VARCHAR}");
        }
        
        if (record.getSpecifiedType() != null) {
            SET("specified_type = #{record.specifiedType,jdbcType=VARCHAR}");
        }
        
        if (record.getStartServiceTime() != null) {
            SET("start_service_time = #{record.startServiceTime,jdbcType=TIMESTAMP}");
        }
        
        if (record.getTemporaryStationId() != null) {
            SET("temporary_station_id = #{record.temporaryStationId,jdbcType=BIGINT}");
        }
        
        if (record.getUserIds() != null) {
            SET("user_ids = #{record.userIds,jdbcType=VARCHAR}");
        }
        
        if (record.getStationId() != null) {
            SET("station_id = #{record.stationId,jdbcType=BIGINT}");
        }
        
        if (record.getUserId() != null) {
            SET("user_id = #{record.userId,jdbcType=BIGINT}");
        }
        
        if (record.getAnswerOther() != null) {
            SET("answer_other = #{record.answerOther,jdbcType=VARCHAR}");
        }
        
        if (record.getIsSupplement() != null) {
            SET("is_supplement = #{record.isSupplement,jdbcType=BIT}");
        }
        
        if (record.getType() != null) {
            SET("type = #{record.type,jdbcType=VARCHAR}");
        }
        
        if (record.getEditUserId() != null) {
            SET("edit_user_id = #{record.editUserId,jdbcType=BIGINT}");
        }
        
        if (record.getUsertype() != null) {
            SET("userType = #{record.usertype,jdbcType=INTEGER}");
        }
        
        applyWhere(example, true);
        return SQL();
    }

    public String updateByExample(Map<String, Object> parameter) {
        BEGIN();
        UPDATE("station_report");
        
        SET("id = #{record.id,jdbcType=BIGINT}");
        SET("creation_time = #{record.creationTime,jdbcType=TIMESTAMP}");
        SET("deleted = #{record.deleted,jdbcType=BIT}");
        SET("enabled = #{record.enabled,jdbcType=BIT}");
        SET("update_time = #{record.updateTime,jdbcType=TIMESTAMP}");
        SET("admin_readed = #{record.adminReaded,jdbcType=BIT}");
        SET("audit_note = #{record.auditNote,jdbcType=VARCHAR}");
        SET("auditing = #{record.auditing,jdbcType=VARCHAR}");
        SET("category = #{record.category,jdbcType=VARCHAR}");
        SET("contact_user = #{record.contactUser,jdbcType=VARCHAR}");
        SET("deparmentIds = #{record.deparmentids,jdbcType=VARCHAR}");
        SET("end_service_time = #{record.endServiceTime,jdbcType=TIMESTAMP}");
        SET("exception_type = #{record.exceptionType,jdbcType=VARCHAR}");
        SET("improvement_time = #{record.improvementTime,jdbcType=BIGINT}");
        SET("is_followed = #{record.isFollowed,jdbcType=BIT}");
        SET("re_audit = #{record.reAudit,jdbcType=BIT}");
        SET("report_category = #{record.reportCategory,jdbcType=VARCHAR}");
        SET("report_type = #{record.reportType,jdbcType=VARCHAR}");
        SET("score = #{record.score,jdbcType=INTEGER}");
        SET("service_type_id = #{record.serviceTypeId,jdbcType=VARCHAR}");
        SET("specified_type = #{record.specifiedType,jdbcType=VARCHAR}");
        SET("start_service_time = #{record.startServiceTime,jdbcType=TIMESTAMP}");
        SET("temporary_station_id = #{record.temporaryStationId,jdbcType=BIGINT}");
        SET("user_ids = #{record.userIds,jdbcType=VARCHAR}");
        SET("station_id = #{record.stationId,jdbcType=BIGINT}");
        SET("user_id = #{record.userId,jdbcType=BIGINT}");
        SET("answer_other = #{record.answerOther,jdbcType=VARCHAR}");
        SET("is_supplement = #{record.isSupplement,jdbcType=BIT}");
        SET("type = #{record.type,jdbcType=VARCHAR}");
        SET("edit_user_id = #{record.editUserId,jdbcType=BIGINT}");
        SET("userType = #{record.usertype,jdbcType=INTEGER}");
        
        StationReportExample example = (StationReportExample) parameter.get("example");
        applyWhere(example, true);
        return SQL();
    }

    public String updateByPrimaryKeySelective(StationReport record) {
        BEGIN();
        UPDATE("station_report");
        
        if (record.getCreationTime() != null) {
            SET("creation_time = #{creationTime,jdbcType=TIMESTAMP}");
        }
        
        if (record.getDeleted() != null) {
            SET("deleted = #{deleted,jdbcType=BIT}");
        }
        
        if (record.getEnabled() != null) {
            SET("enabled = #{enabled,jdbcType=BIT}");
        }
        
        if (record.getUpdateTime() != null) {
            SET("update_time = #{updateTime,jdbcType=TIMESTAMP}");
        }
        
        if (record.getAdminReaded() != null) {
            SET("admin_readed = #{adminReaded,jdbcType=BIT}");
        }
        
        if (record.getAuditNote() != null) {
            SET("audit_note = #{auditNote,jdbcType=VARCHAR}");
        }
        
        if (record.getAuditing() != null) {
            SET("auditing = #{auditing,jdbcType=VARCHAR}");
        }
        
        if (record.getCategory() != null) {
            SET("category = #{category,jdbcType=VARCHAR}");
        }
        
        if (record.getContactUser() != null) {
            SET("contact_user = #{contactUser,jdbcType=VARCHAR}");
        }
        
        if (record.getDeparmentids() != null) {
            SET("deparmentIds = #{deparmentids,jdbcType=VARCHAR}");
        }
        
        if (record.getEndServiceTime() != null) {
            SET("end_service_time = #{endServiceTime,jdbcType=TIMESTAMP}");
        }
        
        if (record.getExceptionType() != null) {
            SET("exception_type = #{exceptionType,jdbcType=VARCHAR}");
        }
        
        if (record.getImprovementTime() != null) {
            SET("improvement_time = #{improvementTime,jdbcType=BIGINT}");
        }
        
        if (record.getIsFollowed() != null) {
            SET("is_followed = #{isFollowed,jdbcType=BIT}");
        }
        
        if (record.getReAudit() != null) {
            SET("re_audit = #{reAudit,jdbcType=BIT}");
        }
        
        if (record.getReportCategory() != null) {
            SET("report_category = #{reportCategory,jdbcType=VARCHAR}");
        }
        
        if (record.getReportType() != null) {
            SET("report_type = #{reportType,jdbcType=VARCHAR}");
        }
        
        if (record.getScore() != null) {
            SET("score = #{score,jdbcType=INTEGER}");
        }
        
        if (record.getServiceTypeId() != null) {
            SET("service_type_id = #{serviceTypeId,jdbcType=VARCHAR}");
        }
        
        if (record.getSpecifiedType() != null) {
            SET("specified_type = #{specifiedType,jdbcType=VARCHAR}");
        }
        
        if (record.getStartServiceTime() != null) {
            SET("start_service_time = #{startServiceTime,jdbcType=TIMESTAMP}");
        }
        
        if (record.getTemporaryStationId() != null) {
            SET("temporary_station_id = #{temporaryStationId,jdbcType=BIGINT}");
        }
        
        if (record.getUserIds() != null) {
            SET("user_ids = #{userIds,jdbcType=VARCHAR}");
        }
        
        if (record.getStationId() != null) {
            SET("station_id = #{stationId,jdbcType=BIGINT}");
        }
        
        if (record.getUserId() != null) {
            SET("user_id = #{userId,jdbcType=BIGINT}");
        }
        
        if (record.getAnswerOther() != null) {
            SET("answer_other = #{answerOther,jdbcType=VARCHAR}");
        }
        
        if (record.getIsSupplement() != null) {
            SET("is_supplement = #{isSupplement,jdbcType=BIT}");
        }
        
        if (record.getType() != null) {
            SET("type = #{type,jdbcType=VARCHAR}");
        }
        
        if (record.getEditUserId() != null) {
            SET("edit_user_id = #{editUserId,jdbcType=BIGINT}");
        }
        
        if (record.getUsertype() != null) {
            SET("userType = #{usertype,jdbcType=INTEGER}");
        }
        
        WHERE("id = #{id,jdbcType=BIGINT}");
        
        return SQL();
    }

    protected void applyWhere(StationReportExample example, boolean includeExamplePhrase) {
        if (example == null) {
            return;
        }
        
        String parmPhrase1;
        String parmPhrase1_th;
        String parmPhrase2;
        String parmPhrase2_th;
        String parmPhrase3;
        String parmPhrase3_th;
        if (includeExamplePhrase) {
            parmPhrase1 = "%s #{example.oredCriteria[%d].allCriteria[%d].value}";
            parmPhrase1_th = "%s #{example.oredCriteria[%d].allCriteria[%d].value,typeHandler=%s}";
            parmPhrase2 = "%s #{example.oredCriteria[%d].allCriteria[%d].value} and #{example.oredCriteria[%d].criteria[%d].secondValue}";
            parmPhrase2_th = "%s #{example.oredCriteria[%d].allCriteria[%d].value,typeHandler=%s} and #{example.oredCriteria[%d].criteria[%d].secondValue,typeHandler=%s}";
            parmPhrase3 = "#{example.oredCriteria[%d].allCriteria[%d].value[%d]}";
            parmPhrase3_th = "#{example.oredCriteria[%d].allCriteria[%d].value[%d],typeHandler=%s}";
        } else {
            parmPhrase1 = "%s #{oredCriteria[%d].allCriteria[%d].value}";
            parmPhrase1_th = "%s #{oredCriteria[%d].allCriteria[%d].value,typeHandler=%s}";
            parmPhrase2 = "%s #{oredCriteria[%d].allCriteria[%d].value} and #{oredCriteria[%d].criteria[%d].secondValue}";
            parmPhrase2_th = "%s #{oredCriteria[%d].allCriteria[%d].value,typeHandler=%s} and #{oredCriteria[%d].criteria[%d].secondValue,typeHandler=%s}";
            parmPhrase3 = "#{oredCriteria[%d].allCriteria[%d].value[%d]}";
            parmPhrase3_th = "#{oredCriteria[%d].allCriteria[%d].value[%d],typeHandler=%s}";
        }
        
        StringBuilder sb = new StringBuilder();
        List<Criteria> oredCriteria = example.getOredCriteria();
        boolean firstCriteria = true;
        for (int i = 0; i < oredCriteria.size(); i++) {
            Criteria criteria = oredCriteria.get(i);
            if (criteria.isValid()) {
                if (firstCriteria) {
                    firstCriteria = false;
                } else {
                    sb.append(" or ");
                }
                
                sb.append('(');
                List<Criterion> criterions = criteria.getAllCriteria();
                boolean firstCriterion = true;
                for (int j = 0; j < criterions.size(); j++) {
                    Criterion criterion = criterions.get(j);
                    if (firstCriterion) {
                        firstCriterion = false;
                    } else {
                        sb.append(" and ");
                    }
                    
                    if (criterion.isNoValue()) {
                        sb.append(criterion.getCondition());
                    } else if (criterion.isSingleValue()) {
                        if (criterion.getTypeHandler() == null) {
                            sb.append(String.format(parmPhrase1, criterion.getCondition(), i, j));
                        } else {
                            sb.append(String.format(parmPhrase1_th, criterion.getCondition(), i, j,criterion.getTypeHandler()));
                        }
                    } else if (criterion.isBetweenValue()) {
                        if (criterion.getTypeHandler() == null) {
                            sb.append(String.format(parmPhrase2, criterion.getCondition(), i, j, i, j));
                        } else {
                            sb.append(String.format(parmPhrase2_th, criterion.getCondition(), i, j, criterion.getTypeHandler(), i, j, criterion.getTypeHandler()));
                        }
                    } else if (criterion.isListValue()) {
                        sb.append(criterion.getCondition());
                        sb.append(" (");
                        List<?> listItems = (List<?>) criterion.getValue();
                        boolean comma = false;
                        for (int k = 0; k < listItems.size(); k++) {
                            if (comma) {
                                sb.append(", ");
                            } else {
                                comma = true;
                            }
                            if (criterion.getTypeHandler() == null) {
                                sb.append(String.format(parmPhrase3, i, j, k));
                            } else {
                                sb.append(String.format(parmPhrase3_th, i, j, k, criterion.getTypeHandler()));
                            }
                        }
                        sb.append(')');
                    }
                }
                sb.append(')');
            }
        }
        
        if (sb.length() > 0) {
            WHERE(sb.toString());
        }
    }
}